package com.htfyun.hardware;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class LedRGBController {

    public enum LedRGBAttr {
        red("red"),
        green("green"),
        blue("blue"),
        ;
        final String name;

        private LedRGBAttr(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public static void setLedOn(LedRGBAttr attr, boolean on) {
        set_led_on(attr, on);
    }

    ////////////////////////////////////////////

    private final static String TAG = "LedController";

    private final static String PATH_GPIO = "/sys/class/htfyun-gpio_leds/gpio";
    private final static String PATH_OFF_LED_IN_SUSPEND = "/sys/class/htfyun-gpio_leds/off_led_in_suspend";


    private static void set_led_on(LedRGBAttr attr, boolean on) {
        if (attr == null) {
            return;
        }
        String cmd = attr.getName() + " " + (on ? "1" : "0");
        led_control(cmd);
    }

    private static void led_control(String cmd) {
        Log.e(TAG, "cmd = " + cmd);
        writeFileContent(PATH_GPIO, cmd);
    }

    private static void set_led_off_in_suspend(LedRGBAttr attr, boolean enabled) {
        String cmd = attr.getName() + " " + (enabled ? "1" : "0");
        writeFileContent(PATH_OFF_LED_IN_SUSPEND, cmd);

    }

    private synchronized static void writeFileContent(String fileName, String content) {
        File file = new File(fileName);
        if (!file.exists()) {
            return;
        }
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(file));
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    private synchronized static List<String> readFileByLineList(String filePath) {
        File file = new File(filePath);

        if (!file.exists()) {
            return null;
        }

        BufferedReader reader = null;

        List<String> stringList = new ArrayList<>();

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                stringList.add(tempString);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return stringList;
    }

}
